import com.geostar.constructionland.zuul.util.aesrsa.*;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.TreeMap;

/**
 * @author yelll
 * @since 2020-1-9 10:04
 **/
@Slf4j
public class aesrsa {
    public static final String clientPrivateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCWRrLc4TI3jsrc2bnmTiWUme44Vhy5V4V0b4hBHykMCKaFRX2x3gprDMHmPW2QgKzS0z57klufPU3Gvz4XrpSZoeVLWb9HO1o83euDI9JsSkduCSh8uTQW3VjkAgRRsNwF8+BP4V2CwRiYnITZhzh9UrI1x+sqCQDbj6qfTSwF1JLyL/D/hcSGnpp5cgxnw4OtFqwZGYjclsqf0/IkN4UWKVM1KOTG+RqRtG41h7PB0SCLeBHkG9AfiGTuXqBVSV/8+nP1DlghknB3Lw0r4FDMYAtx5T8KSp0FIr/eKInCA5RhKfpjd4oofkn1pfDb+/2doMLrO9wJuk/fupUeL4O1AgMBAAECggEAYg/0e+qAKH0HqCJ0CvOH1na0WxLmX5PbRGXblhNNZ8rlTyZPpZftWHnUtffz3U36eQDcTyrsmvcYH8+AJ1gHJFHfHNKN3V+/vORb4RGpKH04K3fSccExWpJKhU9DnkM48LF5+OZpbP+wmzXYA1MX67/hBvNUY/PIGX6gqp9jUNGpn0yxRGXZ04bZ7qTXdx5lx791vH/21blCxdibpYyurQY1PkFI+fyIzUVnaB+ygDKmGCruz5knyZwSdYXYbClCFRgWPLiqUn1IfEzePo858S+FHX1DqSImohU/dWQaeQBg6TWMxdrnwPgtoEdZXv6AFCMn8DLr1y5jhD/vS1eOqQKBgQDk5PBluVZiIk0/m4hD5su1iECNc2NoMgtPxkunI91nyQCjay/1A7bUwfoYbV/ARAjoLe8f3DLq+UkKvlsiy3RlB8B6DAV7l8j08vBaWYodidtWuyL18KjStzXiwNe8FKldoxivBj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E8JHqE0qhb3aXLFVCs3kZlCsNkow6rOIvhtvo79YGr9Fj0FPk6ecq0M/h3+LU4BCUBrzOPVsCn2Vi+exwSzDCwFkIygle+kbqpFCP7Opzh5qwKBgQDLsl2R8U1ClFuCNEqBj0PR5FHfT6ow31L0cMy6EJ9xu/F3KHs/UpyC1N+J2ri5KjkEWBTUUmKbmpqVfJZd8JHMtsT4avhYxORT8njLDxzfNWlwicIPNhQmKMDCMV7ojfLLrfTdtU05nCxphFFRq9fkU50sVR9JYb0EM9DVo/AiDQKBgHQx6B8Xi3cnYdstUN9ra/amLcRjVCrPI6VGiv0/aXfE59nfibQw33a8w3yQu8CUtEEUIcFe53k0uO0gMrb/zDHfeB+P4CmXCUZBG6+vZZsDAkkK9xo+ExZ/3ZAM6sExti6hm3DLBgGe6Y8xADWA1oKCseqfmAdbc2S8lf+rLXU5AoGBAL8CoUIPmgHsl/lygREXGRh5zT2/3EcAsRInEWwMHwuxx1TfI+QPhvaqvXadLj3SYzHu0PS7ZD1v+V8HMRCn1PHfEOkBmOmZFxbmdu8FdqMuk3BEUMwQesvFd9vE5CWgvx8jCZHZj+kJ4lKGI8dFVL53Cmr9BIfYNyJUUwPMR70R";
    public static final String serverPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAm+OCWE4yf9JYMpnRpi5DxqzT9bmeZm0LWc6+jn/7N5Jzg+8Rx7+pXkyUGf9goHDighEcEm4DsJDv/3anHT0vGliP645tJPx3QGJQ1NZdW4mN9k7bC+1v1eUz9IJvaYhH3hJWEhxsbDKcPBzFI0cfbWHt42qYqJZgm2tXy4yRUCiL3ywJxBbkNTilC1e2/ABuZVvRJK2oXdK0j28+pNAZFpuwB8Gom0mZhf50zm4RNp7AsPlxfLxI2EcUj7foQTocXDP46+v8vqXsDlQ4VlYeqDb1f6cUNOLRbl35HdW1H6CF1ngQUlJuyemV6BKA8El8mFG4YkxMH+G3/Uf8fidVtwIDAQAB";

    public static void main(String[] args) throws Exception{
        HashMap<String, Object> params = new HashMap<>();
        params.put("distCodeProvince", "广东省");
        params.put("pagenum", "1");
        params.put("pagesize", "11");
        client(params);
        server();
    }
    static String  reqJson = null;
    public static void client(HashMap<String, Object> params) throws Exception {
        // 生成RSA签名
        String sign = EncryUtil.handleRSA(params, clientPrivateKey);
        params.put("sign", sign);

        String info = new Gson().toJson(params);
        //随机生成AES密钥
        String aesKey = SecureRandomUtil.getRandom(16);
        //AES加密数据
        String data = AES.encryptToBase64(ConvertUtils.stringToHexString(info), aesKey);

//        // 使用RSA算法将商户自己随机生成的AESkey加密
        String encryptkey = RSA.encrypt(aesKey, serverPublicKey);

//        String data = "xJ/kL1AjHPa1aCzqfThKKymnH4b2KwHEDVpfIIwx6dNmfzx5M9ZJ7vzg6iG2X17xZcRUmERQAD1AqsHZeLfbSP86FUrIPDJr4btN/J5MCNUOJh308LHUxxajAU/OeeHSd0vJKZOLo0K3cmURgZm97pQJNgGEL3nrf9Rw1xQIeou1xmITXVm3ZJsc6RrPAX795tADBfANHacITZeI5CqA3M4Q+A4n7djBCefV9y1aOaO8yKDwfnw8FZcb6+MYs8yKTSuz3v74tmyxoz5N1DGceHw/eygC+IxdW1/W6oyx4DIGAmjBtKQ+tZg06owCNZQYMYTq+IONrScZHeNRkV1WgRiFY4E5NBaidXUygqJ5kcgWNkiAY2ojzYrhY9507IUoesQAD7SD6lEcXaVjO1y3miCLmUYKFRg3BjeAjyYU9qypY0cJsrqrM4BcYAV+J47tidtogX/SMRUQqiDWBtMQlPV4VydWRJzUdOsFQB7A1/DHOYZCwukDmjdQPD/3GXpy2Nc8vFVx0qMmJeJ4cvgQZ8BX9DO1Wq8XZr1coL1jert5wE+sDXhL59l8/BHxv7Z6cSJYUtaT8LU5u7V+mP7Wicu+jNnsJm6wtDm6ZFNlDq59uLr7GAnfo960kLrOwK9XWFz1J1sv9eswXT+gkaPVZ2PIljY+NJO3jdQVBIEJJ0loFHTWSyIDc4iJEyX1teNJT5p4L4lwlJgpuUmlPlADZxeekXNMuv0c0/l7tr1BwIPKZiVa7cPFZsuyKBKtptbMUfU2+u+CyDXVIMwBU6KyRP5EDLZXf8Y8l7DCfI1nokMm7+GzlOufJ6LJQ3PNBLste2YCj385nkusjzSrbBxvLAH9lypltG5+JFSHZ0NqjTSkNYq1Lg267OQnIBcdZqRsIG9py4Ia8XFxgetf2jsXplthCdRooVhm628/CiLM31o7/ITVf1B1jAM7dikV8RF9vU02qqtZaoFVN2fdril5/+rqiSkY6mxjc/rlu/rM1puapwITDqIaN3O8YO6YQIFgPz7HSZqkDwgOxFGQKsuPf4x3YtFcA5RRLQ2+XCKaq/ZnYF1yyIpW+2APqIulSaEewWmHknLYvGFpbr8IZZo/9wuenTKEGSmJURyypVYHxocaykJ8iMzh6f6VuGqggGu7";
//        String encryptkey = "IhurF0Nxa/6MbnJwSQkNDb4FBxZ/5Y2abapf2dBpVUlyl3gzR5QSsseCrFfHK3yKg5r2EfGK6X727NfAEknZAA5HF893OAjUT0ndSa7JrwhCOXqEKt7boDOhgfwUD693uCraUS/eFjBC1ZURcSf9YVmWHfJpwZJRBSoMNj22aAQghrAKD9jPwPuScK6O7/dupjIK2DraOnpn6SNXfcmU73NXMrNErN+8zVb0ZkOez5BxdBDIk97fHg51wwzppMmvdjX4lHY3F4pczFp1PTaNc60ND6uPZH2Y2mE2Q3z92HPg791IMfWcR9h6iT+Auj76Cf8rCLWgvB2BcTkVv1MRnA==";
        HashMap<String,String> req = new HashMap<>();
        req.put("data",data);
        req.put("key",encryptkey);
        reqJson = new Gson().toJson(req);
        log.info("data:"+data);
        log.info("key:"+encryptkey);
        System.out.println("加密后的请求数据:\n" + reqJson);
    }

    public static void server() throws Exception {
        HashMap<String ,String> req = new Gson().fromJson(reqJson,new TypeToken<HashMap>(){}.getType());
        // 验签
        boolean passSign = EncryUtil.checkDecryptAndSign(req.get("data"),
                req.get("key"), clientPublicKey, serverPrivateKey);
        if (passSign) {
            // 验签通过
            String aeskey = RSA.decrypt(req.get("key"),
                    serverPrivateKey);
            log.error("aeskey:{}",aeskey);
            String data = ConvertUtils.hexStringToString(AES.decryptFromBase64(req.get("data"),
                    aeskey));

            JsonObject jsonObject = new JsonParser().parse(data).getAsJsonObject();
            String userid = jsonObject.get("distCodeProvince").getAsString();
            String phone = jsonObject.get("pagesize").getAsString();
            System.out.println("解密后的明文:userid:" + userid + " phone:" + phone);
        } else {
            System.out.println("验签失败");
        }
    }
}
